package com.jwell.driverapp.ui.base

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import com.jwell.panprovider.application.UserState
import com.jwell.cargoapp.ui.base.IBaseView
import com.jwell.cargoapp.ui.dialog.LoadingDialog
import com.jwell.cargoapp.ui.dialog.NoTitleDialog
import com.jwell.panprovider.R
import com.jwell.panprovider.application.App
import com.jwell.panprovider.tools.ActivityManager
import com.jwell.panprovider.tools.DataModel
import com.jwell.panprovider.tools.StatuseBarUtils
import com.jwell.panprovider.ui.activity.LoginActivity
import com.jwell.panprovider.ui.activity.MainActivity
import kotlinx.android.synthetic.main.toolbar.*


/**
 * 基础Activity
 */

open class BaseActivity : AppCompatActivity(), IBaseView {

    var dialog: Dialog? = null
    var loading: Dialog? = null
    //按下返回键是否结束该Activity
    protected var isFinish = true
    private lateinit var mListener: OnRightTextListener
    //是否设置不能横屏
    protected var isSetPORTRAIT = true

    fun initToolBar(title: String?, haveBack: Boolean, color: Int?) {
        toolbar.title = ""
        toolbar_title.text = title
        toolbar_title.visibility = View.VISIBLE
        if (haveBack) {
            toolbar_return.visibility = View.VISIBLE
            toolbar_return.setOnClickListener {
                returnActivity()
            }
        }
        if (color != null) {
            toolbar.setBackgroundColor(color)
            StatuseBarUtils.setStatusBarColor(color, this)
        } else {
            toolbar.setBackgroundColor(resources.getColor(R.color.white))
            StatuseBarUtils.setStatusBarColor(resources.getColor(R.color.white), this)
        }
        setSupportActionBar(toolbar)
    }

    fun setRightText(title: String = "") {
        if (title != "") {
            toolbar_right_text_view.visibility = View.VISIBLE
            toolbar_right_text_view.text = title
            toolbar_right_text_view.setOnClickListener(View.OnClickListener {
                if (mListener != null) mListener.setRightOnClick()
            })
        } else {
            toolbar_right_text_view.visibility = View.GONE
        }
    }

    interface OnRightTextListener {
        fun setRightOnClick()
    }

    fun setOnRightOnclickListener(listener: OnRightTextListener) {
        mListener = listener
    }

    protected fun returnActivity() {
        /**隐藏软键盘**/
        hidenEditText()
        ActivityManager.getInstance().popOneActivity(this)
        finish()
        overridePendingTransition(0, R.anim.out_to_right)
    }

    //设置透明状态栏
    protected fun setTransportStatusBar(): Boolean {
        return if (Build.VERSION.SDK_INT >= 21) {
            val decorView = window.decorView
            val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option
            window.statusBarColor = Color.TRANSPARENT
            val actionBar = supportActionBar
            actionBar?.let {
                it.hide()
            }
            true
        } else {
            false
        }
    }

    /**隐藏软键盘**/
    protected fun hidenEditText() {
        val view = window?.peekDecorView()
        if (view != null) {
            val inputmanger = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputmanger.hideSoftInputFromWindow(view?.windowToken, 0)
        }
    }

    override fun showProgress(message: String, outIsTouch: Boolean) {
        try {
            loading = LoadingDialog.Builder(this)
                    .setOutouch(outIsTouch)
                    .setDescrible(message)
                    .creat() as Dialog
            loading?.show()
        } catch (e: Exception) {
            e.printStackTrace()
            loading?.dismiss()
            loading = null
        }
    }

    override fun hideProgress() {
        if (App.DEBUG) println("hideProgress()")
        if (App.DEBUG) println(" loading?" + loading)
        loading?.dismiss()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        savedInstanceState?.clear()
        if (isSetPORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT //设置不能横屏
        }
        super.onCreate(savedInstanceState)
    }

    override fun showDialog(message: String, ensureListener: DialogInterface.OnClickListener?, cancelListener: DialogInterface.OnClickListener?) {
        try {
            dialog = NoTitleDialog.Builder(this)
                    .setMessage(message)
                    .setOnCancelClickListener(cancelListener)
                    .setOnEnsureClickListener(ensureListener)
                    .create()
            dialog?.show()
        } catch (e: Exception) {
            e.printStackTrace()
            dialog?.dismiss()
            dialog = null
        }

    }

    override fun hideDialog() {
        dialog?.hide()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun skipToMain(bundle: Bundle?) {
        var intent = Intent(this, MainActivity::class.java)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
        returnActivity()
    }

    override fun skipToLogin(finish: Boolean) {
        DataModel.getInstance().userState = UserState.LOGOUT
        DataModel.getInstance().removeToken()
        var intent = Intent(this, LoginActivity::class.java).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
        startActivity(intent)
        if (finish) {
            finish
            ActivityManager.getInstance().finishAllActivity()
        }
    }

    override fun toast(message: String) {
        Toast.makeText(App.instance, message, Toast.LENGTH_SHORT).show()
    }
}
